package com.lanou.beautifulwardrobe.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.brand.adapter.BrandListViewAdapter;
import com.lanou.beautifulwardrobe.home.adapter.IntroFRecyclerViewAdapter;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ChoiceProduct;
import com.lanou.beautifulwardrobe.entify.ChoiceResult;
import com.lanou.beautifulwardrobe.entify.IntroData;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class IntroOActivity extends Activity implements View.OnClickListener {
    private ListView introOLv;
    private GridLayoutManager manager;
    private IntroFRecyclerViewAdapter adapter;
    private String url;
    private IntroData data;
    private ImageButton intrOBack;
    private TextView intrOTitle, introODecription;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private BrandListViewAdapter listViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introone);
        this.url = getIntent().getStringExtra("url");
        this.data = (IntroData) getIntent().getSerializableExtra("data");
        initView();
        initData();
    }

    private void initView() {
        introOLv = (ListView) findViewById(R.id.introOLv);
        View header = View.inflate(this, R.layout.activity_introone_header, null);
        introOLv.addHeaderView(header);
        intrOBack = (ImageButton) findViewById(R.id.introOBack);
        intrOTitle = (TextView) findViewById(R.id.introOTitle);
        introODecription = (TextView) header.findViewById(R.id.introODecription);
        listViewAdapter = new BrandListViewAdapter();

        listener();
    }

    private void listener() {
        intrOBack.setOnClickListener(this);
    }

    private void initData() {

        intrOTitle.setText(data.getShort_title());
        introODecription.setText(data.getDescription());

        introOLv.setAdapter(listViewAdapter);
        volleySingleton.getStringRequset(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ChoiceResult choiceResult = gson.fromJson(response, ChoiceResult.class);
                if (choiceResult != null) {
                    final List<ChoiceProduct> choiceProductList = choiceResult.getData().getProducts();
                    listViewAdapter.addData(choiceProductList);

                    introOLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String urlId = choiceProductList.get(position - 1).getTaobao_num_iid();

                            Intent intent = new Intent(IntroOActivity.this, ProductDetailActivity.class);
                            intent.putExtra("id", urlId);
                            startActivity(intent);
                        }
                    });
                }
            }
        });
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.introOBack:
                finish();
                break;
        }

    }


}
